package com.global.college.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseActivity;
import com.global.college.ui.interfaces.IModifyPoneView;
import com.global.college.ui.presenter.ModifyPhonePresenter;
import com.global.college.utils.NetworkUtils;
import com.global.college.utils.RxTimer;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改手机号
 */
public class ModifyPhoneActivity extends BaseActivity implements IModifyPoneView {
    @BindView(R.id.et_sms_code)
    EditText etSmsCode;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.tv_orgin_phone)
    TextView tvOrginPhone;
    private RxTimer rxTimer;
    private String strPhone, strCode;
    private int i = 60;
    private boolean isSend = false;
    private ModifyPhonePresenter modifyPhonePresenter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_modify_phone;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(true);
        setShowBack(true);
        setTitle("修改手机号");
        tvOrginPhone.setText("原手机号: "+UserData.getInstance().getPhone());
        modifyPhonePresenter = new ModifyPhonePresenter(this);
        modifyPhonePresenter.setModifyPoneView(this);
    }

    @Override
    public void getInitData() {
        initListener();
        tvSave.setEnabled(false);
    }

    private void initListener() {
        etSmsCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strCode = charSequence.toString();
                strPhone = etPhone.getText().toString();
                if (StringUtils.isNotEmpty(strCode, true) && StringUtils.isNotEmpty(strPhone, true)) {
                    tvSave.setEnabled(true);
                    tvSave.setBackgroundResource(R.drawable.selector_button_red);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvSave.setEnabled(false);
                    tvSave.setBackgroundResource(R.drawable.selector_button_grey);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strCode = etSmsCode.getText().toString();
                strPhone = charSequence.toString();

                if (StringUtils.isNotEmpty(strCode, true) && StringUtils.isNotEmpty(strPhone, true)) {
                    tvSave.setEnabled(true);
                    tvSave.setBackgroundResource(R.drawable.selector_button_red);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvSave.setEnabled(false);
                    tvSave.setBackgroundResource(R.drawable.selector_button_grey);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_send_code, R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send_code:
                getCode();
                break;
            case R.id.tv_save:
                modifyPhone();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        strPhone = etPhone.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils.isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (!StringUtils.isPhone(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (isSend) {
                ToastUtils.showToastShort(R.string.please_wait_moment);
                return;
            }
            //发送
            modifyPhonePresenter.getSmsCode(strPhone);
            startTimer();
        }
    }

    /**
     * 修改手机号
     */
    private void modifyPhone() {
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showCenterToast(R.string.your_device_has_not_netword);
            return;
        } else {
            strPhone = etPhone.getText().toString();
            strCode = etSmsCode.getText().toString();
            if (StringUtils.isEmpty(strPhone)) {
                ToastUtils.showCenterToast(R.string.please_input_you_phone_number);
                return;
            }
            if (StringUtils.isEmpty(strCode)) {
                ToastUtils.showToastShort(
                        R.string.sms_code_not_empty);
                return;
            } else if (!StringUtils.isNumber(strCode)) {
                ToastUtils.showToastShort(R.string.sms_code_format_error);
                return;
            } else {
                //修改手机号
                modifyPhonePresenter.editPone(strPhone, strCode, UserData.getInstance().getPhone());
            }
        }

    }

    /**
     * 开始计时任务
     */
    private void startTimer() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number) {
                Log.e("打印时间：", "----" + number);
                if (number == i) {
                    tvSendCode.setText(R.string.resend);
                    isSend = false;
                    rxTimer.cancel();
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_fff73043));
                } else {
                    tvSendCode.setText("(" + (i - number) + ")" + getString(R.string.sended));
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_999999));
                    isSend = true;
                }
            }
        });
    }

    @Override
    public void onGetSmsCodeSuccess(Object smsModel) {
        ToastUtils.showCenterToast("发送成功");
    }

    @Override
    public void onGetSmsCodeFail(String msg) {

    }

    @Override
    public void editPhoneSuccess(Object smsModel) {
        ToastUtils.showCenterToast("操作成功");
        finish();
    }

    @Override
    public void editPhoneFail(Object smsModel) {

    }
}